﻿using System;
using System.Collections.Concurrent;
using System.IO;
using System.Net.Sockets;
using System.Threading;

namespace QAQ
{
    class SocClient
    {
        static GCPacket mGCPacket = null;
        public static string mServerIp;
        public static int mServerPort;
        public static bool badass = false;
        Thread mThread;
        private ConcurrentQueue<string> mConcurrentQueue;

        public SocClient(GCPacket mGCP, string ServerIpAddr, int Port)
        {
            mGCPacket = mGCP;
            mServerIp = ServerIpAddr;
            mServerPort = Port;

            mThread = new Thread(new ThreadStart(Service));
            mThread.Start();
        }
        public void Dispose()
        {
            mThread.Abort();
        }

        public static void WritingThread(object o)
        {
            StreamWriter sw = new StreamWriter((Stream) o);
            sw.AutoFlush = true;
            for (; ; )
            {
                if (true == badass)
                {
                    badass = false;
                    mGCPacket.valid = false;
                    sw.WriteLine("BAD");
                }
                if (false == mGCPacket.isValid())
                {
                    sw.WriteLine("G");
                }
                if (true == Form1.GoNow)
                {
                    Form1.GoNow = false;
                    sw.WriteLine("GO!");
                }
                System.Threading.Thread.Sleep(10);
            }
        }

        public static void ReadingThread(object o)
        {
            StreamReader sr = new StreamReader((Stream) o);
            for (;;)
            {
                try
                {
                    switch (sr.ReadLine())
                    {
                        case "HEY":
                            //@@TODO: Queue
                            if (false == mGCPacket.isValid() )
                            {
                                mGCPacket.id = Convert.ToInt32(sr.ReadLine());
                                mGCPacket.pw = sr.ReadLine();
                                mGCPacket.valid = true;
                            }
                            break;
                        case "GG3BE0":
                            //TODO:MUTEX
                            mGCPacket.clear();
                            break;
                        default:
                            break;
                    }
                }
                catch (Exception exc)
                {
                    break;
                }   
            }
        }


        public static void Service()
        {
            TcpClient client;
            client = new TcpClient(mServerIp, mServerPort);

            try
            {
                Stream s = client.GetStream();
                
                Thread wThread = new Thread(new ParameterizedThreadStart(WritingThread) );
                Thread rThread = new Thread(new ParameterizedThreadStart(ReadingThread));
                wThread.IsBackground = true;
                rThread.IsBackground = true;
                wThread.Start(s);
                rThread.Start(s);
                
                wThread.Join();
                rThread.Join();
            }
            catch (Exception exc)
            {
                client.Close();
            }
        }
    }
}
